package org.dfl.plugin.editor.commands;

import java.util.ArrayList;
import java.util.List;

import org.dfl.messages.Messages;
import org.dfl.plugin.editor.model.ConnectionModelElement;
import org.dfl.plugin.editor.model.DflDiagram;
import org.dfl.plugin.editor.model.Shape;
import org.eclipse.gef.commands.Command;

/**
 * command that deletes places and transitions
 * @author Piotr Wlodarczyk
 *
 */
public class ShapeDeleteCommand extends Command {
	/** Shape to remove. */
	private final Shape shape;

	/** ShapeDiagram to remove from. */
	private final DflDiagram parent;

	/** Holds a copy of the outgoing connections of child. */
	private List<ConnectionModelElement> sourceConnections;

	/** Holds a copy of the incoming connections of child. */
	private List<ConnectionModelElement> targetConnections;

	/** True, if child was removed from its parent. */
	private boolean wasRemoved;

	/**
	 * Create a command that will remove the shape from its parent.
	 * 
	 * @param parent
	 *            the ShapesDiagram containing the child
	 * @param child
	 *            the Shape to remove
	 * @throws IllegalArgumentException
	 *             if any parameter is null
	 */
	public ShapeDeleteCommand(DflDiagram parent, Shape child) {
		if (parent == null || child == null) {
			throw new IllegalArgumentException();
		}
		setLabel(Messages.ShapeDeleteCommand_label);
		this.parent = parent;
		this.shape = child;
	}

	/**
	 * Reconnects a List of Connections with their previous endpoints.
	 * 
	 * @param connections
	 *            a non-null List of connections
	 */
	private void addConnections(List<ConnectionModelElement> connections) {
		for (ConnectionModelElement conn :connections) {
			conn.reconnect();
		}
	}
	/**
	 * Disconnects a List of Connections from their endpoints.
	 * 
	 * @param connections
	 *            a non-null List of connections
	 */
	private void removeConnections(List<ConnectionModelElement> connections) {
		for (ConnectionModelElement conn :connections) {
			conn.disconnect();
		}
	}
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#canUndo()
	 */
	public boolean canUndo() {
		return (shape!=null && wasRemoved && shape.getDiagram().isEditable());
	}
	
	@Override
	public boolean canExecute() {
		return (shape!=null && shape.getDiagram().isEditable());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	public void execute() {
		// store a copy of incoming & outgoing connections before proceeding
		sourceConnections = new ArrayList(shape.getSourceConnections());
		targetConnections = new ArrayList(shape.getTargetConnections());
		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	public void redo() {
		// remove the child and disconnect its connections
		wasRemoved = parent.removeChild(shape);
		if (wasRemoved) {
			removeConnections(sourceConnections);
			removeConnections(targetConnections);
		}
	}

	

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	@SuppressWarnings("unchecked") //$NON-NLS-1$
	public void undo() {
		// add the child and reconnect its connections
		if (parent.addChild(shape)) {
			addConnections(sourceConnections);
			addConnections(targetConnections);
		}
	}
}